package view;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import entity.MonthlyCard;
import main.Main;
import util.Constants;
import util.TableData;
import util.TableUtil;
import util.Validator;

public class MonthlyCardJPanel extends JPanel {
	private static final long serialVersionUID = 2543185054428597662L;

	private JTable table;			
	private TableData tableData;
	
	public MonthlyCardJPanel(){
        setLayout(new BorderLayout());
        setBorder(BorderFactory.createTitledBorder("月卡管理"));
        initTopPanel();
        initTablePanel();
	}
	
	private void initTopPanel() {
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BoxLayout(topPanel, BoxLayout.Y_AXIS));
		
		JPanel searchPnl = new JPanel();
		searchPnl.setLayout(new BoxLayout(searchPnl, BoxLayout.X_AXIS));
		
		JPanel inputPnl = new JPanel();
		inputPnl.setLayout(null);

		JLabel lab1 = new JLabel("车牌号",SwingConstants.RIGHT);
		lab1.setFont(Constants.font);
		lab1.setBounds(0, 0, 60, 30);

        JTextField lab1input = new JTextField();
        Validator.bind(lab1input,Validator.CARID);
        lab1input.setBounds(65, 0, 120, 30);

        JLabel lab2 = new JLabel("车位号",SwingConstants.RIGHT);
        lab2.setFont(Constants.font);
        lab2.setBounds(200, 0, 60, 30);
        
        JTextField lab2input = new JTextField();
        lab2input.enableInputMethods(false);
        lab2input.setBounds(265, 0, 120, 30);

        
        inputPnl.add(lab1);
        inputPnl.add(lab1input);

        inputPnl.add(lab2);
        inputPnl.add(lab2input);
        
        
        JPanel btnBar = new JPanel();
		btnBar.setLayout(new BoxLayout(btnBar, BoxLayout.Y_AXIS));
		
		JButton queryBtn = new JButton("查询");
		queryBtn.setFont(Constants.font);
		queryBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String lab1inputStr = lab1input.getText().trim();
				String lab2inputStr = lab2input.getText().trim();
				String[] params = {lab1inputStr,lab2inputStr};
				refreshTable(params);
			}
		});

		JButton resetBtn = new JButton("重置");
		resetBtn.setFont(Constants.font);
		resetBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lab1input.setText("");
			}
		});
		
		btnBar.add(queryBtn);
		btnBar.add(resetBtn);

		searchPnl.add(inputPnl);
		searchPnl.add(btnBar);
		
		JPanel toolBar = new JPanel();
		toolBar.setLayout(new BoxLayout(toolBar, BoxLayout.X_AXIS));
		
		JButton btn1 = new JButton("开卡");
		btn1.setFont(Constants.font);
		btn1.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				JDialog dlg = new JDialog(Main.frame, "选择车位",true);
				Main.dlgStack.push(dlg);
//				dlg.setBounds(300, 200, 800, 600);
				dlg.setSize(800, 600);
				dlg.setLocationRelativeTo(null);
				dlg.add(new CreateMonthlyCardJPanel());
				dlg.validate();
				dlg.addWindowListener(new WindowAdapter() {
		        	@Override
		            public void windowClosing(WindowEvent e) {
		        		if(Main.dlgStack.size() == 1){
		        			Main.dlgStack.pop().dispose();
		        		}
		            }
				});
		        dlg.setVisible(true);
			}
		});
		
		
		JButton btn2 = new JButton("续费");
		btn2.setFont(Constants.font);
		btn2.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int row = table.getSelectedRow();
				if (row < 0) {
					JOptionPane.showMessageDialog(null, "请选择记录!");
					return;
				}
				String id = (String) table.getValueAt(row, 0);
				String state = (String) table.getValueAt(row, 6);
				if ("已失效".equals(state)) {
					JOptionPane.showMessageDialog(null, "卡已失效，无法续费，请重新开卡!");
					return;
				}

				JDialog dlg = new JDialog(Main.frame, "月卡续费",true);
				Main.dlgStack.push(dlg);
//				dlg.setBounds(300, 200, 450, 200);
				dlg.setSize(450, 200);
				dlg.setLocationRelativeTo(null);
				dlg.add(new MonthlyExtendJPanel(id));
				dlg.validate();
				dlg.addWindowListener(new WindowAdapter() {
		        	@Override
		            public void windowClosing(WindowEvent e) {
		        		if(Main.dlgStack.size() == 1){
		        			Main.dlgStack.pop().dispose();
		        		}
		            }
				});
		        dlg.setVisible(true);
			}
		});
		
		toolBar.add(btn1);
		toolBar.add(btn2);
		toolBar.add(Box.createHorizontalGlue());
		
		topPanel.add(searchPnl);
		topPanel.add(toolBar);
		add(topPanel, BorderLayout.NORTH);
	}
	
	private void initTablePanel() {
		String columns[] = { "编号", "车牌号", "车位号", "月数", "开始日期", "结束日期", "状态", "创建时间"};
		List<String[]> rows = new ArrayList<>();
		tableData = new TableData(columns, rows);
		table = new JTable(tableData);
		TableUtil.setTableStyle(table);
		JScrollPane jsPane = new JScrollPane(table);
		TableUtil.setJspStyle(jsPane);
		add(jsPane,BorderLayout.CENTER);
	}
	
	public void refreshTable(String[] params){
		MonthlyCard card = new MonthlyCard();		
		List<String[]> rows = card.query(params);
		tableData.refresh(rows);
	}
}
